using System;
using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	public abstract class MaterialDialog : MonoBehaviour
	{
		private RectTransform m_RectTransform;

		private CanvasGroup m_CanvasGroup;

		private DialogAnimator m_DialogAnimator;

		private bool m_IsModal;

		private bool m_DestroyOnHide = true;

		private Action m_CallbackShowAnimationOver;

		private Action m_CallbackHideAnimationOver;

		public RectTransform rectTransform
		{
			get
			{
				if (m_RectTransform == null)
				{
					m_RectTransform = (base.transform as RectTransform);
				}
				return m_RectTransform;
			}
		}

		public CanvasGroup canvasGroup
		{
			get
			{
				if (m_CanvasGroup == null)
				{
					m_CanvasGroup = base.gameObject.GetAddComponent<CanvasGroup>();
					m_CanvasGroup.blocksRaycasts = false;
				}
				return m_CanvasGroup;
			}
		}

		public DialogAnimator dialogAnimator
		{
			get
			{
				if (m_DialogAnimator == null)
				{
					m_DialogAnimator = new DialogAnimatorSlide();
				}
				return m_DialogAnimator;
			}
			set
			{
				m_DialogAnimator = value;
			}
		}

		public CanvasGroup backgroundCanvasGroup
		{
			get
			{
				if (dialogAnimator != null)
				{
					return dialogAnimator.background;
				}
				return null;
			}
		}

		public bool isModal
		{
			get
			{
				return m_IsModal;
			}
			set
			{
				m_IsModal = value;
			}
		}

		public bool destroyOnHide
		{
			get
			{
				return m_DestroyOnHide;
			}
			set
			{
				m_DestroyOnHide = value;
			}
		}

		public Action callbackShowAnimationOver
		{
			get
			{
				return m_CallbackShowAnimationOver;
			}
			set
			{
				m_CallbackShowAnimationOver = value;
			}
		}

		public Action callbackHideAnimationOver
		{
			get
			{
				return m_CallbackHideAnimationOver;
			}
			set
			{
				m_CallbackHideAnimationOver = value;
			}
		}

		public virtual void Initialize()
		{
			float width = DialogManager.rectTransform.rect.width;
			if (width < 448f)
			{
				RectTransform rectTransform = this.rectTransform;
				float x = width - 48f;
				Vector2 sizeDelta = this.rectTransform.sizeDelta;
				rectTransform.sizeDelta = new Vector2(x, sizeDelta.y);
			}
			else if (width < 648f)
			{
				RectTransform rectTransform2 = this.rectTransform;
				Vector2 sizeDelta2 = this.rectTransform.sizeDelta;
				rectTransform2.sizeDelta = new Vector2(400f, sizeDelta2.y);
			}
			else
			{
				RectTransform rectTransform3 = this.rectTransform;
				Vector2 sizeDelta3 = this.rectTransform.sizeDelta;
				rectTransform3.sizeDelta = new Vector2(600f, sizeDelta3.y);
			}
			base.gameObject.SetActive(value: false);
		}

		public void ShowModal()
		{
			m_IsModal = true;
			Show();
		}

		public virtual void Show()
		{
			base.gameObject.SetActive(value: true);
			LayoutRebuilder.ForceRebuildLayoutImmediate(rectTransform);
			dialogAnimator.dialog = this;
			dialogAnimator.AnimateShow(m_CallbackShowAnimationOver);
			canvasGroup.blocksRaycasts = true;
		}

		public virtual void Hide()
		{
			dialogAnimator.dialog = this;
			dialogAnimator.AnimateHide(delegate
			{
				if (m_CallbackHideAnimationOver != null)
				{
					m_CallbackHideAnimationOver();
				}
				if (m_DestroyOnHide)
				{
					UnityEngine.Object.Destroy(base.gameObject);
				}
				else
				{
					base.gameObject.SetActive(value: false);
				}
			});
			canvasGroup.blocksRaycasts = false;
		}

		public virtual void OnBackgroundClick()
		{
			if (!m_IsModal)
			{
				Hide();
			}
		}
	}
}
